﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.Annotations;

namespace WikiDoc.Utils
{
    /// <summary>
    /// 字符串工具类
    /// </summary>
    public static class StringUtils
    {
        /// <summary>
        /// 获取指定字符串前导空白的个数，空格 TAB 中文空格 等都计为一个空白
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public static int IndentCount([NotNull] this string str)
        {
            if (str == null) throw new ArgumentNullException("str");

            for (int i = 0; i < str.Length; i++)
            {
                if (!char.IsWhiteSpace(str[i]))
                {
                    return i;
                }
            }

            return str.Length;
        }

        /// <summary>
        /// 判断给定的字符串是否是空白，即 null、空字符串 或者仅包含 空白字符
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns>
        ///   <c>true</c> if the specified STR is blank; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsBlank([CanBeNull] this string str)
        {
            if (string.IsNullOrEmpty(str)) return true;

            foreach (var ch in str)
            {
                if(!char.IsWhiteSpace(ch)) return false;
            }

            return true;
        }
    }
}
